<?php

	function dump($d,$stat=false){
		echo '<pre>';
		var_dump($d);
		echo '</pre>';
		if($stat){
			exit;
		}
	}
	require 'MyPdo.class.php';

	$type = $_REQUEST['type'];

	if(!@$type){
		exit('10001');
	}

	switch($type){
		default :echo 'default';break;

		//清除缓存
		case 'clear_cache':
			$cache_uri = @$_POST['cache_uri'];
			if(!$cache_uri){
				exit('err');
			}
			unlink($cache_uri);
			echo 'ok';
			break;


		//删除收藏项或分类
		case 'DelItem':
			$id = intval(@$_POST['id']);
			$cache_uri = @$_POST['cache_uri'];
			$del_type = intval(@$_POST['del_type']);
			if(!$id || !$del_type){
				exit('err1');
			}
			$pdo = MyPdo::init();
			if($del_type == 1){  //删除分类
				$sql_check = 'select id from collect_list where parent = ?';
				$res_check = $pdo->dql($sql_check,array($id),1);
				if($res_check){  //还有子项，不可删除
					exit('exists');
				}
				$sql = 'delete from collect_list where id = ?';
				$res = $pdo->dml($sql,array($id));
			}else{ 					//删除收藏项
				$sql = 'delete from collect_list where id = ?';
				$res = $pdo->dml($sql,array($id));
			}
			if($res){
				file_put_contents($cache_uri,'');
				exit('ok');
			}else{
				exit('err2');
			}
			break;




		//获取收藏数据
		case 'GetInfo':
			$cache_uri = @$_POST['cache_uri'];
			if(!$cache_uri){
				exit('err3');
			}
			$pdo = MyPdo::init();
			$sql = 'select * from collect_list';
			$res = $pdo->dql($sql,array());
			if(!$res){
				$res2 = '[]';
			}
			$data_dir = $data_item = array();
			foreach($res as $v){
				if($v['level'] == 1){	//目录
					$data_dir[] = $v;
				}else{ 				//收藏项
					$data_item[] = $v;
				}
			}
			foreach($data_item as $item){
				foreach($data_dir as $dir_key=>$dir){
					if($dir['id'] == $item['parent']){
						$data_dir[$dir_key]['sub'][] = $item;
					}
				}
			}
			//dump($data_dir);
			echo $res2 = json_encode($data_dir);
			file_put_contents($cache_uri,$res2);
			break;


		//添加收藏项
		case 'add_collect':
			$collect_title = @$_POST['collect_title'];
			$cache_uri = @$_POST['cache_uri'];
			$collect_addr = @$_POST['collect_addr'];
			$add_type = intval(@$_POST['add_type']);  //1 为目录 2 为收藏项
			$parent_id = intval(@$_POST['parent_id']); //收藏的父id
			if(!$collect_title || !$add_type){
				echo $collect_title .'---'. $add_type;
				exit('err1');
			}
			$pdo = MyPdo::init();
			if($add_type == 1){
				$sql_check = 'select id from collect_list where level = 1 and title = ?';
			}else{
				$sql_check = 'select id from collect_list where level = 2 and parent = '.$parent_id.' and title = ?';
			}
			$res_check = $pdo->dql($sql_check,array($collect_title),1);
			if($res_check){
				exit('exists');
			}
			if($add_type == 1){		//1,添加分类 
				$sql = "insert into collect_list values(null,?,'',1,0)";
				$arr = array($collect_title);
			}else{					//2,添加收藏项
				if(!$collect_addr || !$parent_id){
					exit('err2');
				}
				$sql = "insert into collect_list values(null,?,?,2,?)";
				$arr = array($collect_title,$collect_addr,$parent_id);
			}
			$res = $pdo->dml($sql,$arr);
			if($res){
				file_put_contents($cache_uri, '');
				exit('ok');
			}else{
				exit('err3');
			}

			break;


		// 管理员登陆
		case 'login':
			$user = $_POST['user'];
			$pwd = $_POST['pwd'];
			if($user !== 'tpcwre' || $pwd != 'Abcdefg!23'){
			//if($user !== 'tpcwre' || $pwd != 'Abcdefg!23'){
				exit('err');
			}else{
				exit('ok');
			}
			break;


	}



